How do I redirect to another webpage through JavaScript? And how page redirection works?
Redirect to another webpage through JavaScript
257
19-Jul-2021
Updated on 29-Nov-2023
Aryan Kumar
29-Nov-2023You can redirect to another webpage in JavaScript by setting the window.location property to the URL of the destination. Here's an example:
This line of code changes the current location of the browser to the specified URL, effectively redirecting the user to the new page.
You can also use window.location.replace() for a similar effect:
The difference between window.location.href and window.location.replace() is that the latter replaces the current page in the browser's history, making it so that the user cannot navigate back to the previous page using the browser's back button.